<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 会话用户控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\ChatUserService;
use common\validate\Chat;
use common\validate\ChatUser;
use support\ErrorCode;
use think\Request;


class ChatUserController extends VerifyController
{
    /**
     * @var \common\service\ChatUserService
     */
    protected $chatUserService;


    /**
     * ChatUserController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->chatUserService = new ChatUserService();
    }

    /**
     * 邀请用户
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function inviteUser(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->chatUserService->inviteUser($param['data']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 邀请部门或者用户
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function inviteUsersOrDepartment(Request $request)
    {
        $param = $request->param();
        $data = $param['data'];
        $validator = new ChatUser();
        $validator->scene("InviteUsersOrDepartments");
        foreach ($data['receiver'] as $item) {
            if (!$validator->check($item)) {
                throw_strack_exception("[invite_users_or_department] " . $validator->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        try {
            $res = $this->chatUserService->inviteUsersOrDepartment($request->getUserId(), $request->getTenantId(), $param);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 删除用户
     * @param Request $request
     * @return \think\Response
     */
    public function deleteUser(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->chatUserService->deleteUser($param['param'], $request->getUserId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除并退出
     * @param Request $request
     * @return \think\Response
     */
    public function deleteAndExit(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->chatUserService->deleteAndExit($param['param'], $request->getUserId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 转移群主
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function transferAdmin(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->chatUserService->transferAdmin($param['data'], $request->getUserId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新用户配置
     * @param Request $request
     * @return \think\Response
     */
    public function updateByChatGlobalId(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->chatUserService->updateByChatGlobalId($param['data'], $request->getUserId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取邀请的用户
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function getInviteUser(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->chatUserService->getInviteUser($request->getTenantId(), $param);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取邀请的用户详细信息
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function getInviteUserInfo(Request $request)
    {
        $param = $request->param();
        $receiver = $param['data']['receiver'];
        $validator = new ChatUser();
        $validator->scene("InviteUsersOrDepartments");
        foreach ($receiver as $item) {
            if (!$validator->check($item)) {
                throw_strack_exception("[invite_users_or_department] " . $validator->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }

        $receiver = array_group_by($receiver, 'type');
        $userList = $receiver['user'] ?? [];
        if (!empty($userList)) {
            $userList = array_unique(array_column($userList, 'id'));
        }

        $departmentList = $receiver['department'] ?? [];
        if (!empty($departmentList)) {
            $departmentList = array_column($departmentList, 'id');
        }

        $res = $this->chatUserService->getInviteUserInfo($request->getTenantId(), $userList, $departmentList);
        return json(success_response('', $res));
    }

    /**
     * 添加多个用户
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function addMultipleUser(Request $request)
    {
        $data = $request->param()['data'];
        $validator = new ChatUser();
        $validator->scene("AddMultipleUserItem");
        foreach ($data as $item) {
            if (!$validator->check($item)) {
                throw_strack_exception("[add_multiple_user_item] " . $validator->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }

        $res = $this->chatUserService->addMultipleUser($data);
        return json(success_response('', $res));
    }

}